import axios, { AxiosInstance, AxiosResponse } from 'axios';

//返回数据类型
class Result{
  data:any
  code: string = '';
  message: string = '';
  token:string = '';
}
// 登录表单类型定义
export interface LoginForm {
  username: string;
  password: string;
}

// 注册表单类型定义
export interface RegisterForm {
  realName: string,
  username: string;
  password: string;
  identity: string,
  sex: number
  address:string;
}

const baseURL = 'http://localhost:8081';
const api: AxiosInstance = axios.create({
  baseURL,
  headers: {
    Authorization:'Bearer ' + localStorage.getItem('token'),
  },
});

// 登录接口
export const login = (loginForm: LoginForm): Promise<AxiosResponse<Result>> => {
  return api.post('/user/login', loginForm);
};

// 注册接口
export const register = (registerForm: RegisterForm): Promise<AxiosResponse<Result>> => {
  return api.post('/user/register', registerForm);
};

//获取用户信息接口
export const me=():Promise<AxiosResponse<Result>> => {
  return api.get('/user/me');
}

//注销账号接口 
export const logout=():Promise<AxiosResponse<Result>> => {
  return api.post('/user/logout');
}
// 汽车列表分页查询接口
export const getCarPage = (pageParams : any): Promise<AxiosResponse<Result>> => {
  return api.get('/car/page', {params: pageParams})
};

// 汽车出库接口
export const deleteCar = (id : any): Promise<AxiosResponse<Result>> => {
  return api.delete('/car', {params: {id}})
};

// 汽车入库接口
export const addCar = (form : any): Promise<AxiosResponse<Result>> => {
  return api.post('/car', form)
};

// 根据id查询汽车信息接口
export const getCarById = (id : any): Promise<AxiosResponse<Result>> => {
  return api.get('/car', {params: {id}})
};

// 汽车更新接口
export const updateCar = (form : any): Promise<AxiosResponse<Result>> => {
  return api.put('/car', form)
};

// 用户列表分页查询接口
export const getUserPage = (pageParams : any): Promise<AxiosResponse<Result>> => {
  return api.get('/user/page', {params: pageParams})
};

// 根据id查询用户信息接口
export const getUserById = (id : any): Promise<AxiosResponse<Result>> => {
  return api.get('/user', {params: {id}})
};

// 新增用户接口
export const addUser = (form : any): Promise<AxiosResponse<Result>> => {
  return api.post('/user', form)
};

// 用户信息更新接口
export const updateUser = (form : any): Promise<AxiosResponse<Result>> => {
  return api.put('/user', form)
};

// 订单列表分页查询接口
export const getOrderPage = (pageParams : any): Promise<AxiosResponse<Result>> => {
  return api.get('/order/page', {params: pageParams})
};

// 订单信息更新接口
export const updateOrder = (form : any): Promise<AxiosResponse<Result>> => {
  return api.put('/order', form)
};

// 根据id查询订单信息接口
export const getOrderById = (id : any): Promise<AxiosResponse<Result>> => {
  return api.get('/order', {params: {id}})
};

// 根据id查询检查单信息接口
export const getCheckById = (id : any): Promise<AxiosResponse<Result>> => {
  return api.get('/check', {params: {id}})
};

// 根据车辆id查询车辆所有检查单信息接口
export const getCheckByCarId = (carId : any): Promise<AxiosResponse<Result>> => {
  return api.get('/check/' + carId)
};

// 检查单信息更新接口
export const updateCheck = (form : any): Promise<AxiosResponse<Result>> => {
  return api.put('/check', form)
};

// 检查单列表分页查询接口
export const getCheckPage = (pageParams : any): Promise<AxiosResponse<Result>> => {
  return api.get('/check/page', {params: pageParams})
};

// 查询当前用户信息接口
export const getCurrentUserInfo = (): Promise<AxiosResponse<Result>> => {
  return api.get('/user/me')
};

// 根据用户id查询所有订单接口
export const getAllOrderByUserId = (userId : any): Promise<AxiosResponse<Result>> => {
  return api.get('/order/getAllByUserId',{params: {userId}})
};

//生成订单
export const addOrder=(form : any): Promise<AxiosResponse<Result>> => {
  return api.post('/order', form)
};

// 新增用户接口
export const addCheck = (form : any): Promise<AxiosResponse<Result>> => {
  return api.post('/check', form)
};

// 根据车辆id查询最新检查单信息接口
export const getOneCheckByCarId = (carId : any): Promise<AxiosResponse<Result>> => {
  return api.get('/check/new/' + carId)
};